package com.woniuxy.microfixeddepositserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microfixeddepositserver.bean.FixedTimeDepositBean;
import com.woniuxy.microfixeddepositserver.bean.detail.RegularQuery;
import com.woniuxy.microfixeddepositserver.service.IFixedDespositService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.joda.time.DateTime;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author: masterhai
 * @date: 2021/4/7:11:03
 * @description: 定期存款控制层
 */
@RefreshScope
@Api("定期理财产品功能接口")
@RestController
@Slf4j
@RequestMapping(value = "/fixedDeposit", produces = {"application/json;charset=utf-8"})
public class FixedDepositController {
    @Resource
    private IFixedDespositService fixedDespositService;

    /**
     * 根据id下架或者删除该理财产品
     */
    @ApiOperation("下架或者删除定期理财产品")
    @PostMapping("remove")
    public ResultBean deleteByPrimaryKey (@RequestBody String[] checkList) {
        if (checkList == null || checkList.length < 1) {
            return ResultBean.ParamError();
        }
        fixedDespositService.deleteByPrimaryKey(Arrays.asList(checkList));
        return ResultBean.builder().code(200).msg("下架或删除产品操作成功").build();
    }

    /**
     * 根据id下上架该理财产品
     */
    @ApiOperation("上架定期理财产品")
    @PostMapping("recover")
    public ResultBean recoverFixedDeposit (@RequestBody String[] checkList) {
        if (checkList == null || checkList.length < 1) {
            return ResultBean.ParamError();
        }
        fixedDespositService.recoverFixedDeposit(Arrays.asList(checkList));
        return ResultBean.builder().code(200).msg("上架产品成功").build();
    }

    /**
     * 新增理财产品
     */
    @ApiOperation("新增定期理财产品")
    @PutMapping("add")
    public ResultBean insert (@ApiParam("定期理财产品对象") @RequestBody FixedTimeDepositBean fixedTimeDeposit) {
        log.info("参数:" + fixedTimeDeposit);
        if (fixedTimeDeposit != null && fixedTimeDeposit.getYieldRates() != 0.0) {
            fixedDespositService.insert(fixedTimeDeposit);
            return ResultBean.builder().code(200).msg("新增成功").build();
        }
        return ResultBean.ParamError();
    }


    /**
     * 根据id查询理财产品
     */
    @ApiOperation("根据id查询定期理财产品")
    @PostMapping("findOne")
    public ResultBean selectByPrimaryKey (@ApiParam("查询对象") @RequestBody RegularQuery regularQuery) {
        if (ObjectUtils.isEmpty(regularQuery)) {
            return ResultBean.ParamError();
        }
        FixedTimeDepositBean depositBean = fixedDespositService.selectByPrimaryKey(regularQuery.getFid(), regularQuery.getStatus());
        depositBean.setPurchasingArray(depositBean.getPurchasing().split(","));
        return ResultBean.builder().code(200).msg("查询数据成功").data(depositBean).build();
    }


    /**
     * 分页查询所有理财产品信息
     *
     * @return
     */
    @ApiOperation("分页查询所有定期理财产品(包括热卖产品)")
    @PostMapping("findByAny")
    public ResultBean selectAll (@ApiParam("查询对象") @RequestBody RegularQuery regularQuery) {
        String startTime = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        if (ObjectUtils.isEmpty(regularQuery)) {
            return ResultBean.ParamError();
        }
        Integer status = (regularQuery.getStatus() == null || regularQuery.getStatus() == 0) ? 1 : regularQuery.getStatus();
        PageInfo<FixedTimeDepositBean> pageInfo = null;
        //Integer isHot = regularQuery.getIsHot() == null ? 2 : regularQuery.getIsHot();
        if (regularQuery.getIsHot() == null || regularQuery.getIsHot() == 0) {
            //查询所有定期产品
            pageInfo = fixedDespositService.selectAllProduct(regularQuery.getPage(), regularQuery.getSize(), status);
        } else {
            //查询热门或者非热门商品
            pageInfo = fixedDespositService.selectAll(regularQuery.getPage(), regularQuery.getSize(), status, regularQuery.getIsHot());
        }

        String endTime = new DateTime().toString("yyyy-MM-dd HH:mm:ss");

        return ResultBean.builder().code(200).msg("查询数据成功").data(pageInfo).build();
    }


    /**
     * 分页条件查询所有理财产品信息
     *
     * @return
     */
    @ApiOperation("分页条件查询所有定期理财产品")
    @PostMapping("findByAnyQuery")
    public ResultBean selectAllQuery (@ApiParam("查询对象") @RequestBody RegularQuery regularQuery) {
        if (regularQuery == null || regularQuery.getPage() < 0 || regularQuery.getSize() < 0) {
            return ResultBean.ParamError();
        }

        //根据条件查询
        PageInfo<FixedTimeDepositBean> pageInfo = fixedDespositService.selectAllByQuery(regularQuery);
        return ResultBean.builder().code(200).msg("查询数据成功").data(pageInfo).build();
    }

    /**
     * 修改理财产品信息
     */
    @ApiOperation("修改定期理财产品信息")
    @PutMapping("update")
    public ResultBean updateByPrimaryKey (@ApiParam("定期理财产品对象") @RequestBody FixedTimeDepositBean fixedTimeDeposit) {
        if (fixedTimeDeposit == null || "".equals(fixedTimeDeposit)) {
            return ResultBean.ParamError();
        }
        fixedDespositService.updateByPrimaryKey(fixedTimeDeposit);
        return ResultBean.builder().code(200).msg("修改数据成功").build();
    }


    /**
     * 修改理财产品信息
     */
    @ApiOperation("明星产品状态修改")
    @PutMapping("updateIsHot")
    public ResultBean updateIsHot (@ApiParam("查询对象") @RequestBody RegularQuery regularQuery) {
        if (ObjectUtils.isEmpty(regularQuery)) {
            return ResultBean.ParamError();
        }
        fixedDespositService.updateIsHot(regularQuery);
        return ResultBean.builder().code(200).msg("修改数据成功").build();
    }
}
